package com.suitsoft.bdms.entity.account;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.suitsoft.bdms.entity.IdEntity;
import com.suitsoft.bdms.entity.system.TimeAuthority;

/**
 * 权限.
 * 
 * 注释见{@link User}.
 * 
 * @author calvin
 */
@Entity
@Table(name = "ACCT_AUTHORITY")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Authority extends IdEntity {

	/**
	 * SpringSecurity中默认的角色/授权名前缀.
	 */
	public static final String AUTHORITY_PREFIX = "ROLE_";

	private String name;
	
	private Long groupId;
	
	private String supName;
	
	private Integer effective;
	
	private Set<TimeAuthority> timeAuthority = new HashSet<TimeAuthority>();

	public Authority() {
	}

	@OneToMany(fetch = FetchType.LAZY,mappedBy = "authority")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Set<TimeAuthority> getTimeAuthority() {
		return timeAuthority;
	}

	public void setTimeAuthority(Set<TimeAuthority> timeAuthority) {
		this.timeAuthority = timeAuthority;
	}

	public Authority(Long id, String name) {
		this.id = id;
		this.name = name;
	}

	@Column(nullable = false, unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Transient
	public String getPrefixedName() {
		return AUTHORITY_PREFIX + name;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public Long getGroupId() {
		return groupId;
	}
	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}

	public String getSupName() {
		return supName;
	}

	public void setSupName(String supName) {
		this.supName = supName;
	}

	public Integer getEffective() {
		return effective;
	}

	public void setEffective(Integer effective) {
		this.effective = effective;
	}
}
